/*

微博下面的toolbar视图 任务如下:
* 布局按钮 和 中间的分割线
* 判断按钮中文字的显示逻辑

*/

import UIKit

class HRHomePageToolBar: UIView {
    //  将当前按钮所在cell的位置赋值给按钮的tag值
    var index: Int = 0{
        didSet{
            print(index)
            reposts_count.tag = index
        }
    }
    
    var homePageStatusViewModel: HRHomePageStatusViewModel?{
        //  通过属性观察器给按钮的title赋值,(赋值数据的格式已经在模型中做好逻辑判断)
        didSet{
            reposts_count.setTitle(homePageStatusViewModel?.retweetStr, forState: .Normal)
            comments_count.setTitle(homePageStatusViewModel?.commentStr, forState: .Normal)
            unlikeButton.setTitle(homePageStatusViewModel?.likeStr, forState: .Normal)

        }
    }
     // MARK: - 懒加载控件
    //  按钮
    private lazy var reposts_count: UIButton = self.addChildButton( "转发", imageName: "timeline_icon_retweet")
    private lazy var comments_count: UIButton = self.addChildButton( "评论", imageName: "timeline_icon_comment")
     private lazy var unlikeButton: UIButton = self.addChildButton( "赞", imageName: "timeline_icon_unlike")
    //  分割线
    private lazy var seperateLine1: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    private lazy var seperateLine2: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     // MARK: - 添加控件,添加约束
    private func setupUI(){
        //  给转发按钮添加点击事件
        reposts_count.addTarget(self, action: "repostsWeibo:", forControlEvents: .TouchUpInside)
        
        reposts_count.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(comments_count)
        }
        comments_count.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(reposts_count.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(comments_count.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
        //  坑: 一定要注意线的添加顺序,如果顺序不对会被覆盖,怎么也看不见的
        addSubview(seperateLine1)
        addSubview(seperateLine2)
        
        seperateLine1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(reposts_count.snp_trailing)
            make.centerY.equalTo(self)
        }
        seperateLine2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(comments_count.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        
    }
     // MARK: - 转发按钮的点击事件
    @objc private func repostsWeibo(sender: UIButton){
        print(sender.tag)
        let accesstoken = HRUserAccountInfos.sharedUserAccount.access_token
        //  获取转发微博的id
       let id = homePageStatusViewModel?.homePageModel?.id
        print(id)
        let url = "https://api.weibo.com/2/statuses/repost.json"
        
        let params: [String: AnyObject] = [
        "access_token": accesstoken!,
            "id": "\(id)"
        
        ]
        //  通知控制器发送网络请求?????请求失败
        HRNetworkingTool.sharedSessionManager.requestForResource(.POST, url: url, parameters: params) { (response, error) -> Void in
            print(response)
            print(error)
        }
    }
    
    
    
     // MARK: - 创建按钮的公用方法
    private func addChildButton(title: String, imageName: String) -> UIButton{
        //  设置btn的属性
        let btn = UIButton()
        btn.setTitle(title, forState: .Normal)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        //  消除点击按钮时候的高亮效果
        btn.adjustsImageWhenHighlighted = false
        //        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Highlighted)
//        btn.backgroundColor = UIColor(patternImage: UIImage(named: "timeline_card_bottom_background")!)
        //  添加到toolbar视图
        addSubview(btn)
        
        return btn
    }
    
}
